package poker.ai;

import java.util.ArrayList;

import enums.ActionType;

import poker.Action;
import poker.Card;
import poker.GameRound;
import poker.HandPowerRating;
import poker.Player;
import poker.Printer;
import poker.Rules;

public class StupidAI extends Player
{
	ActionType chosenActionType;
	Action action;
	int actionNumber;
	boolean playerTypeConservative;

	/**
	 * A very basic AI player. Pre-flop choices are based on randomness, post-flop choices are based on the current hand's power rating.
	 * @param startCash
	 * @param name
	 */
	public StupidAI(int startCash, String name, boolean conservative) 
	{
		super(startCash, name);

		chosenActionType = null;
		action = null;
		actionNumber = 0;
		playerTypeConservative = conservative;
	}

	@Override
	public ActionType chooseAction(GameRound gameRound)
	{
		ArrayList<ActionType> legalActions = Rules.getLegalActions(this);
		Printer.printAvailbleActions(this, legalActions);
		Printer.printValueToCall(this, gameRound.getCurrentBet() - getPostedCash());

		if(gameRound.getIsPreFlop())
		{
			//Random AI action preFlop

			actionNumber = legalActions.size();
			int randomChose = (int)(Math.random() * actionNumber);
			chosenActionType = legalActions.get(randomChose);
		}

		if(gameRound.getFlopDealt())
		{			
			Card[] hand = gameRound.getHand(this);


			////LIBERAL PLAYER
			if(!playerTypeConservative){
				//A fold hand
				if(HandPowerRating.rate(hand)[0] <= 1) 
				{
					if(legalActions.contains(ActionType.CHECK))
					{
						chosenActionType = ActionType.CHECK;
					}
					else
						chosenActionType = ActionType.FOLD; 
				}

				//A call hand
				if(HandPowerRating.rate(gameRound.getHand(this))[0] >= 2 &&
						HandPowerRating.rate(gameRound.getHand(this))[0] < 4) 
				{	
					if(legalActions.contains(ActionType.CHECK))
					{
						chosenActionType = ActionType.CHECK;
					}
					else
						chosenActionType= ActionType.CALL; 
				}	

				//A raise hand
				if(HandPowerRating.rate(gameRound.getHand(this))[0] >= 4 &&
						HandPowerRating.rate(gameRound.getHand(this))[0] < 10) 
				{
					if(legalActions.contains(ActionType.RAISE))
						chosenActionType = ActionType.RAISE;
					else
						chosenActionType= ActionType.CALL; 
				}
			}

			////CONSERVATIVE PLAYER
			if(playerTypeConservative){
				//A fold hand
				if(HandPowerRating.rate(hand)[0] <= 2) 
				{
					if(legalActions.contains(ActionType.CHECK))
					{
						chosenActionType = ActionType.CHECK;
					}
					else
						chosenActionType = ActionType.FOLD; 
				}

				//A call hand
				if(HandPowerRating.rate(gameRound.getHand(this))[0] >= 3 &&
						HandPowerRating.rate(gameRound.getHand(this))[0] <= 4) 
				{	
					if(legalActions.contains(ActionType.CHECK))
					{
						chosenActionType = ActionType.CHECK;
					}
					else
						chosenActionType= ActionType.CALL; 
				}	

				//A raise hand
				if(HandPowerRating.rate(gameRound.getHand(this))[0] > 4 &&
						HandPowerRating.rate(gameRound.getHand(this))[0] < 10) 
				{
					if(legalActions.contains(ActionType.RAISE))
						chosenActionType = ActionType.RAISE;
					else
						chosenActionType= ActionType.CALL; 
				}
			}
		}
		return chosenActionType;
	}
}
